#include "key.h"
#include "User_Main.h"
#include "RS485.h"
#include "stdio.h"
#include "bsp_timer.h"
#include "tim.h"
#include "modbus_host.h"

void BSP_HardTimer_Test(u8 ucKeyCode);
void BSP_SoftTimer_Test(u8 ucKeyCode);
void bsp_ModBus_Test(u8 ucKeyCode);



void BSP_SoftTimer_Test(u8 ucKeyCode)
{
	if (!ucKeyCode )return;
	switch (ucKeyCode)
	{
		case KEY1:
		{

			u8 data[5];
			for (size_t i = 0; i < 5; i++)
				data[i]	=i+1;	
			RS485_SendBuf(data,5);
		}break;
		case KEY3:
		{
			bsp_StartTimer(0,1000);

		}break;
		case KEY4:
		{
			bsp_StartAutoTimer(1,1000);

		}break;
		default:{}break;
	}
}

void UserKey()
{
	u8 ucKeyCode=0;
	ucKeyCode=Get_keyVal();
	if (!ucKeyCode )return;

	// BSP_SoftTimer_Test (ucKeyCode);
	// BSP_HardTimer_Test (ucKeyCode);
	bsp_ModBus_Test (ucKeyCode);

	CLR_keyVal(0xff);

}



void user_main()
{
	UserKey();

//--------------------------------------------
	if( bsp_CheckTimer(0) )
	{
		LED1_Toggle;
		bsp_StartTimer(0,500);
	}
	if( bsp_CheckTimer(1) )
	{
		static u8 t=0;

		bsp_StopTimer(0);
		LED1_OFF;
		LED2_Toggle;

		if(++t ==10){
			bsp_StopTimer(1);
			t=0;
		}
	}

}

//---------------------------------------------------
void led_flash()
{
	static u16 t=0;
	if(2*10 ==++t)
	{
		LED2_Toggle;
		t=0;
	}

	BSP_HARDTIM_START_AGAIN(1);
}


void BSP_HardTimer_Test(u8 ucKeyCode)
{

	if (!ucKeyCode )return;

	switch (ucKeyCode)
	{
		case KEY1:
		{
			u8 data[5];
			for (size_t i = 0; i < 5; i++)
				data[i]	=i+1;	
			RS485_SendBuf(data,5);
			bsp_StartHardTimer(1,50000,led_flash);//50ms
		}break;
		default:{}break;
	}
}
//-------------------------------------------------
void bsp_ModBus_Test(u8 ucKeyCode)
{

	if (!ucKeyCode )return;
	switch (ucKeyCode)
	{
		case KEY1:
		{
			/*写(1个) 
					写(1个)  	从机地址  功能码    寄存器地址	   写入的值		校验码
					发送(Hex)  		01 		 01		   01 01 		00 04 		6D F5
			 */
			if (MODH_ReadParam_01H(REG_D01, 4) == 1) ;
			else printf("通信失败\n");
		}break;
		case KEY3:
		{
			 if (MODH_ReadParam_01H(1, 1) == 1) ;
			else printf("通信失败\n");

		}break;
		case KEY4:
		{
			 

		}break;
		case KEY5:
		{
			 

		}break;
		case KEY6:
		{
			 

		}break;
		case KEY2:
		{
			 

		}break;
		default:{}break;
	}



}
